﻿using System;
using System.Collections.Generic;
using System.Linq;

using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.Edu
{
    public partial class PartyEduHonourEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyEduHonourAdd";
            }
        }


        #endregion

        #region 页面属性
        public string EditorContent
        {
            get; set; }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(rblFlags, SysDataDictionary.ArticleFlags);
            ControlBusiness.ControlBindDataDictionary(ddlCategory, SysDataDictionary.EduHonour);
            rblFlags.SelectedIndex = 0;
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtTitle.Text = article.Title;
                rblFlags.SelectedValue = article.Flags.ToString();
                ddlCategory.SelectedValue = article.Category;
                txtSummary.Text = article.Summary;
                cbxEnabled.Checked = article.Status == 4;
                EditorContent = article.Content;
                hdFileId.Text = article.ImageID;
                if (!string.IsNullOrEmpty(hdFileId.Text))
                {
                    filePhoto.EmptyText = UpFileBusiness.GetFileName(hdFileId.Text);
                    HyperLink1.ImageID = hdFileId.Text;
                }
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;
            PartyArticle article;
            if (id != "")
            {
                //更新
                article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                article.ModifyTime = DateTime.Now;
                article.ModifyBy = CurrentUser.LoginID;
            }
            else
            {
                article = new PartyArticle();
                article.ID = StringHelper.NewID;
                article.CreateTime = DateTime.Now;
                article.ModifyTime = DateTime.Now;
                article.CreateBy = CurrentUser.LoginID;
                article.ModifyBy = CurrentUser.LoginID;
                DB.PartyArticles.Add(article);
            }

            article.Module = SysDataDictionary.EduHonour;
            article.Category = ddlCategory.SelectedValue;
            article.Title = txtTitle.Text.Trim();
            article.Subhead = txtSummary.Text.Trim();
            article.Status = cbxEnabled.Checked ? 4 : 1;
            article.Summary = txtSummary.Text.Trim();
            article.Flags = Convert.ToInt32(rblFlags.SelectedValue);
            article.Content = this.Request.Form["editorValue"];
            if (!string.IsNullOrEmpty(hdFileId.Text))
            {
                article.ImageID = hdFileId.Text;
            }
            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID, SysDataDictionary.EduHonour);
            if (rs.IsSuccess)
            {
                hdFileId.Text = rs.FileID;
                HyperLink1.ImageID= rs.FileID;


            }
            else
            {
                Alert.Show("上传失败！" + rs.Message, String.Empty);
            }
        }
        #endregion
    }
}